<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>DijkstraUndirectedSP.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="DijkstraUndirectedSP code in Java">
<meta NAME="TITLE" CONTENT="DijkstraUndirectedSP code in Java">
<meta NAME="KEYWORDS" CONTENT="DijkstraUndirectedSP,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>DijkstraUndirectedSP.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "DijkstraUndirectedSP.java">DijkstraUndirectedSP.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac DijkstraUndirectedSP.java</span>
<span class="comment"> *  Execution:    java DijkstraUndirectedSP input.txt s</span>
<span class="comment"> *  Dependencies: EdgeWeightedGraph.java IndexMinPQ.java Stack.java Edge.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/43mst/tinyEWG.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/43mst/mediumEWG.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/43mst/largeEWG.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Dijkstra's algorithm. Computes the shortest path tree.</span>
<span class="comment"> *  Assumes all weights are nonnegative.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java DijkstraUndirectedSP tinyEWG.txt 6</span>
<span class="comment"> *  6 to 0 (0.58)  6-0 0.58000</span>
<span class="comment"> *  6 to 1 (0.76)  6-2 0.40000   1-2 0.36000</span>
<span class="comment"> *  6 to 2 (0.40)  6-2 0.40000</span>
<span class="comment"> *  6 to 3 (0.52)  3-6 0.52000</span>
<span class="comment"> *  6 to 4 (0.93)  6-4 0.93000</span>
<span class="comment"> *  6 to 5 (1.02)  6-2 0.40000   2-7 0.34000   5-7 0.28000</span>
<span class="comment"> *  6 to 6 (0.00)</span>
<span class="comment"> *  6 to 7 (0.74)  6-2 0.40000   2-7 0.34000</span>
<span class="comment"> *</span>
<span class="comment"> *  % java DijkstraUndirectedSP mediumEWG.txt 0</span>
<span class="comment"> *  0 to 0 (0.00)</span>
<span class="comment"> *  0 to 1 (0.71)  0-44 0.06471   44-93  0.06793  ...   1-107 0.07484</span>
<span class="comment"> *  0 to 2 (0.65)  0-44 0.06471   44-231 0.10384  ...   2-42  0.11456</span>
<span class="comment"> *  0 to 3 (0.46)  0-97 0.07705   97-248 0.08598  ...   3-45  0.11902</span>
<span class="comment"> *  ...</span>
<span class="comment"> *</span>
<span class="comment"> *  % java DijkstraUndirectedSP largeEWG.txt 0</span>
<span class="comment"> *  0 to 0 (0.00)  </span>
<span class="comment"> *  0 to 1 (0.78)  0-460790 0.00190  460790-696678 0.00173   ...   1-826350 0.00191</span>
<span class="comment"> *  0 to 2 (0.61)  0-15786  0.00130  15786-53370   0.00113   ...   2-793420 0.00040</span>
<span class="comment"> *  0 to 3 (0.31)  0-460790 0.00190  460790-752483 0.00194   ...   3-698373 0.00172</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>


<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DijkstraUndirectedSP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for solving</span>
<span class="comment"> *  the single-source shortest paths problem in edge-weighted graphs</span>
<span class="comment"> *  where the edge weights are nonnegative.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses Dijkstra's algorithm with a binary heap.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> log </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">distTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">hasPathTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> methods take</span>
<span class="comment"> *  constant time and the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">pathTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method takes time proportional to the</span>
<span class="comment"> *  number of edges in the shortest path returned.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,    </span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/44sp"</span><span class="keyword">&gt;</span><span class="comment">Section 4.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of    </span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne. </span>
<span class="comment"> *  See {</span><span class="type">@link</span><span class="comment"> DijkstraSP} for a version on edge-weighted digraphs.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Nate Liu</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">DijkstraUndirectedSP</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> distTo</span><span class="symbol">;</span><span class="normal">          </span><span class="comment">// distTo[v] = distance  of shortest s-&gt;v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> Edge</span><span class="symbol">[]</span><span class="normal"> edgeTo</span><span class="symbol">;</span><span class="normal">            </span><span class="comment">// edgeTo[v] = last edge on shortest s-&gt;v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">IndexMinPQ&lt;Double&gt;</span><span class="normal"> pq</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// priority queue of vertices</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes a shortest-paths tree from the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to every</span>
<span class="comment">     * other vertex in the edge-weighted graph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  G the edge-weighted digraph</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  s the source vertex</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if an edge weight is negative</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> - 1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">DijkstraUndirectedSP</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" has negative weight"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        distTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        edgeTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Edge</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>
<span class="normal">        distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// relax vertices in order of distance from s</span>
<span class="normal">        pq </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> IndexMinPQ</span><span class="symbol">&lt;</span><span class="normal">Double</span><span class="symbol">&gt;(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">());</span>
<span class="normal">        pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span>
<span class="normal">                </span><span class="function">relax</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check optimality conditions</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// relax edge e and update pq if changed</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">relax</span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">            edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">))</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">decreaseKey</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the length of a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and</span>
<span class="comment">     * vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the length of a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and</span>
<span class="comment">     *         the vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.POSITIVE_INFINITY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">distTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if there is a path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and</span>
<span class="comment">     * vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is a path between the source vertex</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  v the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Edge&gt;</span><span class="normal"> </span><span class="function">pathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Stack&lt;Edge&gt;</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span><span class="normal"> e </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">x</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">            x </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> path</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// check optimality conditions:</span>
<span class="normal">    </span><span class="comment">// (i) for all edges e = v-w:            distTo[w] &lt;= distTo[v] + e.weight()</span>
<span class="normal">    </span><span class="comment">// (ii) for all edge e = v-w on the SPT: distTo[w] == distTo[v] + e.weight()</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// check that edge weights are nonnegative</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"negative edge weight detected"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that distTo[v] and edgeTo[v] are consistent</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0.0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo[s] and edgeTo[s] inconsistent"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">==</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo[] and edgeTo[] inconsistent"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that all edges e = v-w satisfy distTo[w] &lt;= distTo[v] + e.weight()</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" not relaxed"</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that all edges e = v-w on SPT satisfy distTo[w] == distTo[v] + e.weight()</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">];</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">w </span><span class="symbol">!=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> w </span><span class="symbol">!=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">()))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" on shortest path not tight"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DijkstraUndirectedSP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedGraph</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>

<span class="normal">        </span><span class="comment">// compute shortest paths</span>
<span class="normal">        </span><span class="usertype">DijkstraUndirectedSP</span><span class="normal"> sp </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DijkstraUndirectedSP</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>


<span class="normal">        </span><span class="comment">// print shortest path</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> t </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> t</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sp</span><span class="symbol">.</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d (%.2f)  "</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> t</span><span class="symbol">,</span><span class="normal"> sp</span><span class="symbol">.</span><span class="function">distTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">));</span>
<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> sp</span><span class="symbol">.</span><span class="function">pathTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">e </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"   "</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d         no path</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> t</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
